package me.zhengjie.modules.other.rest;

import com.qiaofang.applet.service.other.domain.LendingRates;
import com.qiaofang.applet.service.other.service.LendingRatesService;
import com.qiaofang.applet.service.other.service.dto.LendingRatesAdminDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.utils.SecurityUtils;
import me.zhengjie.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: youqing.yang
 * @Create: 2020/11/13 11:21
 * @Description:
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "房贷计算：贷款利率")
@RequestMapping("/api/lendingRates")
public class LendingRatesController {

    @Autowired
    private LendingRatesService lendingRatesService;

    @GetMapping
    @ApiOperation(value = "查询贷款利率", notes = "查询贷款利率")
    public ResponseEntity<LendingRatesAdminDto> list() {
        return ResponseEntity.ok(lendingRatesService.getLendingRatesForAdmin(SecurityUtils.getCorporationCode()));
    }

    @PostMapping
    @ApiOperation(value = "新增贷款利率", notes = "新增贷款利率")
    public ResponseEntity<Object> save(@RequestBody LendingRatesAdminDto lendingRatesAdminDto) {
        LendingRates lendingRates = new LendingRates();
        BeanUtils.copyProperties(lendingRatesAdminDto, lendingRates);
        if (StringUtils.isBlank(lendingRatesAdminDto.getId())){
            lendingRates.setCorporationCode(SecurityUtils.getCorporationCode());
        }
        lendingRatesService.saveLendingRates(lendingRates);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping("/listHistoricalData")
    @ApiOperation(value = "查看历史利率", notes = "查看历史利率")
    public ResponseEntity<Object> listHistoricalData() {
        return ResponseEntity.ok(lendingRatesService.listHistoricalData(SecurityUtils.getCorporationCode()));
    }
}
